package com.javaenzk.wfx.manager.controller;

import com.javaenzk.wfx.manager.po.SysModule;
import com.javaenzk.wfx.manager.po.User_Info;
import com.javaenzk.wfx.manager.service.UserService;
import com.javaenzk.wfx.manager.util.ResultJson;
import com.javaenzk.wfx.manager.vo.ParentNode;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserControl {

    @Resource
    UserService userService;

    /**
     * 登陆的链接
     * @param username
     * @param password
     * @return
     */
    @RequestMapping("/login")
    @ResponseBody
    public ResultJson login(String username, String password){
        try {
            SecurityUtils.getSubject().login(new UsernamePasswordToken(username,password));
            ResultJson.createSuccessResult(null);
        } catch (AuthenticationException e) {
            e.printStackTrace();
            return ResultJson.createFailResult("1","error");
        }
        return ResultJson.createSuccessResult(null);
    }

    /**
     * 后台管理页面首页查询权限数据
     * @return
     */
    @RequestMapping("/sysmodu")
    public String queryModule(Model model){

        try {
            User_Info user= (User_Info) SecurityUtils.getSubject().getSession().getAttribute("user");
            List<SysModule>  hasnosysModules = userService.queryHasNoModule(user.getAccount());
            List<SysModule>  hassysModules = userService.queryHasModule(user.getAccount());
            model.addAttribute("hassysModules",hassysModules);
            model.addAttribute("hasnosysModules",hasnosysModules);
        } catch (Exception e) {
            e.printStackTrace();
        }

         return "permission";
    }

    /**
     * 查询树节点数据
     * @return
     * @throws Exception
     */
    @RequestMapping("/parantNode")
    @ResponseBody
    public ResultJson queryParent(Model model) throws  Exception{

      User_Info user= (User_Info) SecurityUtils.getSubject().getSession().getAttribute("user");

        final List<ParentNode> parentNodes = userService.queryParent(user.getRoleId());

        return ResultJson.createSuccessResult(parentNodes);
    }

    /**
     * 增加权限
     * @param moduleId
     * @param
     * @return
     */
    @RequestMapping("/addfunc")
    @ResponseBody
    public ResultJson addsysmodule(String[] moduleId)
    {
        try {
            User_Info user= (User_Info) SecurityUtils.getSubject().getSession().getAttribute("user");
            userService.addSysmodule(moduleId,user.getUserId());
            return ResultJson.createSuccessResult(null);
        }catch (Exception e){
            e.printStackTrace();
            return ResultJson.createFailResult("1","error");
        }
    }

    /**
     * 删除权限
     * @param moduleCode
     * @param
     * @return
     */
    @RequestMapping("/delfunc")
    @ResponseBody
    public ResultJson delsysmodule(String[] moduleCode){

        try {
            User_Info user= (User_Info) SecurityUtils.getSubject().getSession().getAttribute("user");
            userService.delSysmodule(moduleCode,user.getRoleId());
            return ResultJson.createSuccessResult(null);
        }catch (Exception e){
            e.printStackTrace();
            return ResultJson.createFailResult("1","error");
        }


    }

}
